package com.atguigu.crowd.mvc.config;

import com.atguigu.crowd.exception.LoginAcctAlreadyInUseException;
import com.atguigu.crowd.exception.LoginAcctAlreadyInUseForUpdateException;
import com.atguigu.crowd.exception.AccessForbiddenException;
import com.atguigu.crowd.exception.LoginFailedException;
import com.atguigu.crowd.util.CrowdUtil;
import com.atguigu.crowd.util.CrowdConstant;
import com.atguigu.crowd.util.ResultEntity;
import com.google.gson.Gson;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author yzt
 * @create 2021-09-11 15:34
 */
@ControllerAdvice
public class CrowdExceptionResolver {

    //空指针异常处理
    @ExceptionHandler(value = NullPointerException.class)
    public ModelAndView nullPointerException(
            NullPointerException exception,
            HttpServletRequest request,
            HttpServletResponse response) throws IOException {
        String viewName =  "system-error";
        return commonResolve(viewName,exception,request,response);
    }
    //登陆失败异常处理
    @ExceptionHandler(value = LoginFailedException.class)
    public ModelAndView loginFailedException(
            LoginFailedException exception,
            HttpServletRequest request,
            HttpServletResponse response) throws IOException {

        String viewName = "admin-login";

        return commonResolve(viewName,exception,request,response);
    }
    //禁止访问异常处理
    @ExceptionHandler(value = AccessForbiddenException.class)
    public ModelAndView AccessForbiddenException(
            AccessForbiddenException exception,
            HttpServletRequest request,
            HttpServletResponse response) throws IOException {

        String viewName = "admin-login";

        return commonResolve(viewName,exception,request,response);
    }

    //新增用户时账号重复异常处理
    @ExceptionHandler(value = LoginAcctAlreadyInUseException.class)
    public ModelAndView LoginAcctAlreadyInUseException(
            LoginAcctAlreadyInUseException exception,
            HttpServletRequest request,
            HttpServletResponse response) throws IOException {
        String viewName =  "admin-add";
        return commonResolve(viewName,exception,request,response);
    }
    //更新用户时账号重复异常处理
    @ExceptionHandler(value = LoginAcctAlreadyInUseForUpdateException.class)
    public ModelAndView LoginAcctAlreadyInUseForUpdateException(
            LoginAcctAlreadyInUseForUpdateException exception,
            HttpServletRequest request,
            HttpServletResponse response) throws IOException {
        String viewName =  "system-error";
        return commonResolve(viewName,exception,request,response);
    }
    // 创建通用方法
    private ModelAndView commonResolve(
                    String viewName, Exception exception,
                    HttpServletRequest request,
                    HttpServletResponse response ) throws IOException {

        boolean judge = CrowdUtil.judgeRequestType(request);
        if (judge){
            ResultEntity<Object> resultEntity = ResultEntity.failed(exception.getMessage());
            Gson gson = new Gson();
            String json = gson.toJson(resultEntity);
            response.getWriter().write(json);
            return null;
        }
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject(CrowdConstant.ATTR_NAME_EXCEPTION,exception);
        modelAndView.setViewName(viewName);
        return modelAndView;
    }
}
